 <?php

$freq = array(0 => "1");
$number = '11112';
$value = [];
$chars = str_split($number);
$newpos = array(0 => 0, 1 => 1);
$result = [];
foreach ($newpos as $p) {
	$char = $chars[$p];

	$matched = false;

	$maxLian = getCharConsec($chars, $char, $value, $matched);

	if (empty($value)) {
		if (count($freq) > 0) {
			if (in_array(getCombinationToStringPos1($chars, array($p)), $freq)) {
				$result[] = $number;
				continue;
			}
		} else {
			$result[] = $number;
			continue;
		}
	}
}
var_dump($result);

function getCharConsec($chars, $c, $value, &$matched) {
	if (!in_array($c, $chars)) {
		return 0;
	}

	// 字符是否在 $value 中
	$matched = false;

	// 连最大次数
	$result = 0;

	// 正向搜索
	$search_asc = function ($chars, $c, $value, &$result, &$matched) use (&$search_asc) {
		// 升序搜索，遇到10，则当作0
		if ($c == 10) {
			$c = 0;
		}
		if (in_array($c, $chars)) {
			if (!$matched) {
				$matched = in_array($c, $value);
			}
			++$result;
			return $search_asc($chars, $c + 1, $value, $result, $matched);
		}
	};

	// 逆向搜索
	$search_dsc = function ($chars, $c, $value, &$result, &$matched) use (&$search_dsc) {
		if (in_array($c, $chars)) {
			// 降序搜索，如果遇到0，当成10处理
			if ($c == 0) {
				$c = 10;
			}

			if (!$matched) {
				$matched = in_array($c, $value);
			}
			++$result;
			return $search_dsc($chars, $c - 1, $value, $result, $matched);
		}
	};

	$search_asc($chars, $c, $value, $result, $matched);
	$search_dsc($chars, $c, $value, $result, $matched);

	return $result - 1;
}

function getCombinationToStringPos1($arr, $pos): int{
	$cnt = 0;
	if (count($pos) <= 2) {
		//1,2位取并集
		foreach ($pos as $key => $val) {
			for ($i = 0; $i < count($arr); $i++) {
				if ($i == $val) {
					continue;
				}
				//$result[] = $arr[$pos - 1] . $arr[$i];  //与其它位数字是连,统计次数
				if (abs($arr[$val] - $arr[$i]) == 9 or abs($arr[$val] - $arr[$i]) == 1) {
					$cnt++;
				}
			}
		}
	} elseif (count($pos) == 3) {
		#比如位置123的组合12,13,23
		if (abs($arr[$pos[0]] - $arr[$pos[1]]) == 9 or abs($arr[$pos[0]] - $arr[$pos[1]]) == 1) //12的组合判断是否连
		{
			$cnt++;
		}
		if (abs($arr[$pos[0]] - $arr[$pos[2]]) == 9 or abs($arr[$pos[0]] - $arr[$pos[2]]) == 1) //13的组合判断是否连
		{
			$cnt++;
		}
		if (abs($arr[$pos[1]] - $arr[$pos[2]]) == 9 or abs($arr[$pos[1]] - $arr[$pos[2]]) == 1) //23的组合判断是否连
		{
			$cnt++;
		}
	} elseif (count($pos) == 4) {
		#比如位置123的组合12,13,14,23,24,34
		if (abs($arr[$pos[0]] - $arr[$pos[1]]) == 9 or abs($arr[$pos[0]] - $arr[$pos[1]]) == 1) //12的组合判断是否连
		{
			$cnt++;
		}
		if (abs($arr[$pos[0]] - $arr[$pos[2]]) == 9 or abs($arr[$pos[0]] - $arr[$pos[2]]) == 1) //13的组合判断是否连
		{
			$cnt++;
		}
		if (abs($arr[$pos[0]] - $arr[$pos[3]]) == 9 or abs($arr[$pos[0]] - $arr[$pos[3]]) == 1) //14的组合判断是否连
		{
			$cnt++;
		}
		if (abs($arr[$pos[1]] - $arr[$pos[2]]) == 9 or abs($arr[$pos[1]] - $arr[$pos[2]]) == 1) //23的组合判断是否连
		{
			$cnt++;
		}
		if (abs($arr[$pos[1]] - $arr[$pos[3]]) == 9 or abs($arr[$pos[1]] - $arr[$pos[3]]) == 1) //24的组合判断是否连
		{
			$cnt++;
		}
		if (abs($arr[$pos[2]] - $arr[$pos[3]]) == 9 or abs($arr[$pos[2]] - $arr[$pos[3]]) == 1) //34的组合判断是否连
		{
			$cnt++;
		}
	}
	return $cnt;

}